﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Interop;


namespace WGM_Client_Net35
{
 
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        [DllImport("user32.dll")]
        private static extern int FlashWindowEx(ref FLASHWINFO pwfi);
        private struct FLASHWINFO
        {
            public Int32 cbSize;
            public IntPtr hwnd;
            public Int32 dwFlags;
            public Int32 uCount;
            public Int32 dwTimeout;
        }

        Hashtable _allMessages = new Hashtable();
        ObservableCollection<Message> _messages;
        ObservableCollection<TabListItem> _tabs;
        ObservableCollection<ContactListItem> _contactListItems;
        public Color SystemColour { get; set; }
        public Window1()
        {
            InitializeComponent();
            SystemColour = Colors.Red;
            _messages = new ObservableCollection<Message>();
            _allMessages.Add("main", _messages);
            _tabs = new ObservableCollection<TabListItem>();
            _tabControl.ItemsSource = _tabs;
            TabListItem mainTab = new TabListItem("Main Conversation", DateTime.Now.ToString("ff"), "main", _messages, Colors.Green.ToString());
            mainTab.CloseButtonVisibility = Visibility.Collapsed;
            _tabs.Add(mainTab);
            _contactListItems = new ObservableCollection<ContactListItem>();
            _contactListView.ItemsSource = _contactListItems;
            
            _contactListItems.Add(new ContactListItem("testuser1","mystatus","testuser1",null));
            _contactListItems.Add(new ContactListItem("no status", null, "no status user", null));

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

            string userName = "User:"+DateTime.Now.ToString("ss-ffff");
            TabListItem newItem = new TabListItem(userName, userName, userName, null, Colors.Red.ToString());

            _tabs.Add(newItem);
            newItem.IsTabSelected = true;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Message newMessage = new Message(DateTime.Now, "button click message",Colors.Navy, "global_sender", Visibility.Visible, "Blue");
            
            ReceiveMessage("main", newMessage);
        }
        private void ReceiveMessage(string userName, Message message)
        {
            foreach (TabListItem tli in _tabs)
            {
                if (tli.OutgoingTBName == userName)
                {
                    
                    tli.AddMessage(message);
                }
            }
        }
        private void TextBox_KeyUp(object sender, KeyEventArgs e)
        {
            BindableRichTextBox t = sender as BindableRichTextBox;
            if (e.Key == Key.Enter && e.KeyboardDevice.Modifiers == ModifierKeys.Control)
            {
                t.CaretPosition.InsertLineBreak();
                
                t.CaretPosition = t.CaretPosition.DocumentEnd;
            }
            if (e.Key == Key.Enter && e.KeyboardDevice.Modifiers == ModifierKeys.None)
            {
                TextPointer from = t.CaretPosition.DocumentStart;
                TextPointer to = t.CaretPosition;
                t.Selection.Select(from, to);
                t.Selection.Text = t.Selection.Text.Substring(0, t.Selection.Text.Length - 2);
                
                string text = t.Selection.Text;
                
                string user = t.Tag as string;
                try
                {
                    Message newMessage = new Message(DateTime.Now, text,Colors.Navy, user, Visibility.Visible, "Blue");
                    ReceiveMessage(user, newMessage);
                }
                catch
                {
                    Message newMessage = new Message(DateTime.Now, text,Colors.Navy, "global_sender", Visibility.Visible, "Blue");
                    user = "main";
                    ReceiveMessage(user, newMessage);
                }
                t.SelectAll();
                t.Selection.Text = "";
            }            
        }
        private void Tab_CloseButton_Click(object sender, RoutedEventArgs e)
        {

        }
        private void BindableRichTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            BindableRichTextBox rtb = sender as BindableRichTextBox;
            rtb.ScrollToEnd();
            if(this.IsActive != true)
                FlashWindow(new WindowInteropHelper(this).Handle);
        }
        public void FlashWindow(IntPtr handle)
        {
            //int FLASHW_STOP = 0;
            //int FLASHW_CAPTION = 1;
            //int FLASHW_TRAY = 2;
            int FLASHW_ALL = 3;
            //int FLASHW_TIMER = 4;
            int FLASHW_TIMERNOFG = 12;
            FLASHWINFO pwfi = new FLASHWINFO();
            pwfi.cbSize = Marshal.SizeOf(pwfi);
            pwfi.hwnd = handle;
            pwfi.dwFlags = FLASHW_ALL | FLASHW_TIMERNOFG;
            pwfi.uCount = 3;
            pwfi.dwTimeout = 0;
            Int32 intRet = FlashWindowEx(ref pwfi);
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            _contactListItems.Add(new ContactListItem(DateTime.Now.ToString("user: ss-ffff"), "", "no status user", null));
        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            _contactListItems[1].FlashContactItem();
            _contactListItems[1].DisplayNameColour = Colors.LightGreen.ToString();
            _contactListItems[1].DisplayHighlightColour = Colors.LightGreen.ToString();
            _tabs[1].TabHeaderColour = Colors.Black.ToString();
            _tabs[1].TabHeaderText = "Offline";
            _tabs[1].TabOpacity = 0.5;
        }

        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
            if(_tabs[1].IsTabSelected == false)
                _tabs[1].FlashTabItem();
        }

        private void MenuItem_Click_3(object sender, RoutedEventArgs e)
        {
            ObservableCollection<Message> incoming = new ObservableCollection<Message>();
            TabListItem newTabItem =  new TabListItem("test user", "test", "test", incoming, Colors.Blue.ToString());
            _tabs.Add(newTabItem);
            newTabItem.IsTabSelected = true;
        }

        private void viewbox_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Viewbox clickedBtn = sender as Viewbox;
            string userName = clickedBtn.Tag as string;
            for (int count = 0; count < _tabs.Count; count++)
            {
                if (_tabs[count].OutgoingTBName == userName && count != 0)
                    _tabs.RemoveAt(count);
            }
        }




    }

}
